﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration;
using System.Text;
using NLCER;

namespace NLServices
{
    partial class NLTskServices : ServiceBase
    {
        #region Constructor
        public NLTskServices()
        {
            InitializeComponent();
            this.ServiceName = "NLTskServices";
        }
        #endregion

        #region Field
        private static System.Timers.Timer curtimer;
        private static NLSMain nlchecker;
        #endregion

        #region Process Services State
        protected override void OnStart(string[] args)
        {
            try
            {
                if (null == curtimer)
                {
                    curtimer = new System.Timers.Timer();
                }

                if (null == nlchecker)
                {
                    nlchecker = new NLSMain();
                }

                int invel = 9000;
                if (int.TryParse(ConfigurationManager.AppSettings["TimerTick"].ToString(), out invel))
                    curtimer.Interval = invel;
                else
                    curtimer.Interval = 9000;
                curtimer.Elapsed += CurTimer_Tick;
                curtimer.Start();
            }
            catch (Exception exp)
            {
                WinLog.Addlog(exp.Message, "NLTskServices");
            }

        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
        }

        private void CurTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                ConfigurationManager.AppSettings["Timer"] = DateTime.Now.ToString();
                //WinLog.Addlog("Build task list...", "NLTskServices");
                NLSMain.BuildTsk();
                //WinLog.Addlog("end of build task,waiting for next..", "NLTskServices");
            }
            catch (Exception exp)
            {
                WinLog.Addlog(exp.Message, "NLTskServices");
            }
        }
        #endregion
    }
}
